package taskmaster.util;
/**
 This checks the OCI JDBC installation.
 Run it and provide the connect information.
 It selects your Oracle session roles.
 */


// java.sql package to use JDBC; java.io to read the command line

import NewTmas.FramesandPanels.LoginFrame;
import oracle.jdbc.driver.OracleDriver;

import java.io.IOException;
import java.sql.*;
//import oracle.jdbc.driver.OracleDriver;


class ConTest
{

    public static void main(String args[])
            throws SQLException, IOException, ClassNotFoundException
    {
        LoginFrame.printClassLoaderAndEnv();

        DriverManager.registerDriver(new OracleDriver());

        // Prompt the user for connect information
        System.out.println("OCI JDBC connection test");


        String user = "";
        String password = "";
        String database = "tman.prod";


//      user = getInput("db user: ");
//      int slash_index = user.indexOf('/');
//      if (slash_index != -1) {
//         password = user.substring(slash_index + 1);
//         user     = user.substring(0, slash_index);
//      }
//      else password = getInput("password: ");
//      database      = getInput("service: ");


        // Connect to the database
        System.out.print("Connecting to the database...");
        System.out.flush();
        System.out.println("Connecting...");
        Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@" + database, user, password);
        System.out.println("connected.");


        // Get user name from the database
        Statement name_stmt = conn.createStatement();
        ResultSet name_rset = name_stmt.executeQuery("select username from user_users");
        System.out.println("\nYour Session username :");
        while (name_rset.next())
        {
            System.out.println(name_rset.getString(1));
        }
        name_stmt.close();
        name_rset.close();


        // Get session roles from the database
        Statement ses_stmt = conn.createStatement();
        ResultSet ses_rset = ses_stmt.executeQuery("select role from session_roles");
        System.out.println("\nYour Session has role(s) :");
        while (ses_rset.next())
        {
            System.out.println(ses_rset.getString(1));
        }
        // close the result set and statement
        ses_rset.close();
        ses_stmt.close();


        // close the connection
        conn.close();
        System.out.println("\nYour OCI JDBC works OK.");
    }


    // Utility function to read a line from standard input
    static String getInput(String prompt)
    {
        try
        {

            StringBuffer buffer = new StringBuffer();
            System.out.print(prompt);
            System.out.flush();


            int c = System.in.read();
            while (c != '\n' && c != -1)
            {
                buffer.append((char) c);
                c = System.in.read();
            }
            return buffer.toString().trim();
        }

        catch (IOException e)
        {
            return "";
        }
    }

}
